﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Mani.Common.Security;

namespace Mani.Common.UI
{
    public partial class Login : System.Web.UI.UserControl
    {
        public IAuthorizationProvider auth { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (auth == null)
                auth = new WebAuthorizationProvider();

            if (!Page.IsPostBack)
            {
                FirstTimeInstall installer = new FirstTimeInstall(Membership.ApplicationName, true);
                
                List<Entities> user_entites = new List<Entities>();
                user_entites.Add(new Entities { Description = "روزنامه ها"   , Name = "Newspapers" });
                user_entites.Add(new Entities { Description = "هواپیمایی ها" , Name = "Airlines" });
                user_entites.Add(new Entities { Description = "مسیرهای پرواز", Name = "Transfers" });
                user_entites.Add(new Entities { Description = "نوع بسته بندی ها", Name = "QuantityUnits" });
                user_entites.Add(new Entities { Description = "بارنامه ها", Name = "Orders" });
                user_entites.Add(new Entities { Description = "نوع پرداختی ها", Name = "PaymentTypes" });
                user_entites.Add(new Entities { Description = "پرداختی ها", Name = "Payments" });
                user_entites.Add(new Entities { Description = "حسابداری ها", Name = "Accountings" });

                installer.user_entites = user_entites;
                

                if (installer.IsFirstTime())
                    installer.InitializeForFirstTime();
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {

            bool authenticated = auth.Login(UserName.Text, Password.Text);

            if (authenticated)
            {
                FormsAuthentication.Initialize();

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName.Text, DateTime.Now,
                                                                                 DateTime.Now.AddMinutes(10),
                                                                                 true, null, FormsAuthentication.FormsCookiePath);

                string hash = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

                // Set the cookie's expiration time to the tickets expiration time
                if (ticket.IsPersistent)
                    cookie.Expires = ticket.Expiration;

                // Add the cookie to the list for outgoing response
                Response.Cookies.Add(cookie);
                FormsAuthentication.RedirectFromLoginPage(UserName.Text, true);
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                UIHelper.ClientAlert(this.Page, "شناسه کاربری یا رمز عبور معتبر نبود لطفا مجددا تلاش کنید");
            }
        }
    }
}